package com.xyg.easybuy.spring;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.sf.ehcache.Ehcache;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.cache.Cache.ValueWrapper;

public class SpringCacheManagerWrapper implements CacheManager {

    private org.springframework.cache.CacheManager cacheManager;

    /**
     * 设置spring cache manager
     *
     * @param cacheManager
     */
    public void setCacheManager(org.springframework.cache.CacheManager cacheManager) {
        this.cacheManager = cacheManager;
    }

	@Override
    public <K, V> Cache<K, V> getCache(String name) throws CacheException {
        org.springframework.cache.Cache springCache = cacheManager.getCache(name);
        return new MySpringCacheWrapper<K,V>(springCache);
    }

	static class MySpringCacheWrapper<K,V> implements Cache<K, V> {
		private org.springframework.cache.Cache springCache;
		

		public org.springframework.cache.Cache getSpringCache() {
			return springCache;
		}

		public void setSpringCache(org.springframework.cache.Cache springCache) {
			this.springCache = springCache;
		}

		MySpringCacheWrapper(org.springframework.cache.Cache springCache) {
            this.springCache = springCache;
        }

		@SuppressWarnings("unchecked")
		@Override
		public V get(K key) throws CacheException {
			ValueWrapper value = springCache.get(key);
			if (value == null) return null;
			return (V) value.get();
		}

		@Override
		public V put(K key, V value) throws CacheException {
			springCache.put(key, value);
			return value;
		}

		@Override
		public V remove(K key) throws CacheException {
			V value = get(key);
			if (value != null) {
				springCache.evict(key);
			}
			return value;
		}

		@Override
		public void clear() throws CacheException {
			springCache.clear();
		}

		@Override
		public int size() {
			 if(springCache.getNativeCache() instanceof Ehcache) {
	                Ehcache ehcache = (Ehcache) springCache.getNativeCache();
	                return ehcache.getSize();
	            }
	         throw new UnsupportedOperationException("invoke spring cache abstract size method not supported");
		}

		@SuppressWarnings("unchecked")
		@Override
		public Set<K> keys() {
			 if(springCache.getNativeCache() instanceof Ehcache) {
	                Ehcache ehcache = (Ehcache) springCache.getNativeCache();
	                return new HashSet<K>(ehcache.getKeys());
	            }
	         throw new UnsupportedOperationException("invoke spring cache abstract keys method not supported");
		}

		@Override
		public Collection<V> values() {
            if(springCache.getNativeCache() instanceof Ehcache) {
                Ehcache ehcache = (Ehcache) springCache.getNativeCache();
                List<?> keys = ehcache.getKeys();
                if (!CollectionUtils.isEmpty(keys)) {
                    List<V> values = new ArrayList<>(keys.size());
                    for (Object key : keys) {
                        @SuppressWarnings("unchecked")
						V value =  get( (K) key);
                        if (value != null) {
                            values.add(value);
                        }
                    }
                    return Collections.unmodifiableList(values);
                } else {
                    return Collections.emptyList();
                }
            }
            throw new UnsupportedOperationException("invoke spring cache abstract values method not supported");
        
		}
		
	}
	
}
